function showPage(pageId,isPopup){
	if(isPopup) {
			$id("body").removeClass("hideScroll");
			$id("popup").removeClass("hide");
		} else {
			if($id(currentPage)){
				$id(currentPage).addClass("hide");
			}
		}
		if(isPopup) {
			
		} 
		$id(pageId).removeClass("hide");
		lastPage = currentPage;
		currentPage = pageId;	

}

function hidePopup () {
  		$id(currentPage).addClass("hide");
		$id("popup").addClass("hide");
		$id("body").removeClass("hideScroll");
		currentPage = lastPage;
}

function showLoading() {
		$id("popup").removeClass(this.hideClass);
		$id("loadingView").removeClass(this.hideClass);	
	}
	
function hideLoading() {
		$id("popup").addClass(this.hideClass);
		$id("loadingView").addClass(this.hideClass);	
	}

var cssHide = "hide";

function load() {

	setEvents();
	callServiceHotBook();
    CartFunction.setText();
	showPage("content-index");
	//pagesManager.setPage(Pages.home, false);
	ajax.get({
		url : "http://localhost:8080/book-service/public/book/authen/info",
		success : function(data, xhr) {
			console.log(data);

			$id("login").addClass(cssHide);
			$id("profile").removeClass(cssHide);
			$id("panel").addClass(cssHide);

			var userName = data.getElementsByTagName("userName")[0].childNodes[0].nodeValue;
			var avatar = data.getElementsByTagName("avatar")[0].childNodes[0].nodeValue;
			var email = data.getElementsByTagName("email")[0].childNodes[0].nodeValue;
			currentUserEmail = email;
			currentUserId=userName;
			$id("profileUserName").innerText = userName.substring(0, 5);
			$id("icon-avatar").src = avatar;
			$id("profilePicture").src = avatar;
			var key = "home";
			addActive(key);
			//sessionStorage.userName=userName.substring(0,5);
		},
		failure : function(xhr) {
			console.log("fail");

		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
};

function doPleaseLogin() {
	alert("please log in to access");
	$id("panel").removeClass(cssHide);
	$id("btnLogin").addClass("active");
}



function uploadFiles(event) {
	var formData = new FormData();
	var file = event.currentTarget[0].files[0];
	formData.append("file", file);
	formData.append("backendUrl", "demo2");

	var xhr = new XMLHttpRequest();
	xhr.open('POST', "http://localhost:8080/book-service/user/changeAvatar", true);
	xhr.onload = function(e) {
		alert("oke");
	};

	xhr.send(formData);
	// multipart/form-data
	alert("return");
	return false;
}

function addActive(key) {
	var arr = ["home", "books", "about"];
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == key) {
			$id(key).addClass("active");
			continue;
		}
		$id(arr[i]).removeClass("active");

	}
}

function formatDateTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year + "/"
        + (month < 10 ? "0" + month : month) + "/"
        + (day < 10 ? "0" + day : day) + " "
        + (hour < 10 ? "0" + hour : hour) + ":"
        + (minute < 10 ? "0" + minute : minute) + ":"
        + (second < 10 ? "0" + second : second) + ":"
}

function isValidUserName(){
	var userName = $id("txtUsernameId").value;
	if(userName.length<5){
		$id("invalidMessageId").innerText ="UserName cần nhiều hơn 5 ký tự";
		$id("btnRegisterId").disabled = true;
	}else if(userName.indexOf(" ") != -1){
		$id("invalidMessageId").innerText ="UserName không được chứa khoảng trắng";
		$id("btnRegisterId").disabled = true;
	}
	else{
	//var isExist=true;
		ajax.get({
		url : "http://localhost:8080/book-service/public/book/authen/checkExits",
		param:{
			"userName":userName
		},
		success : function(data, xhr) {
			console.log(data);
			isExist = data.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			if(isExist=="true"){
				$id("invalidMessageId").innerText ="Username này đã được sử dụng";
				$id("btnRegisterId").disabled = true;
				//alert(isExist);
			}else{
				$id("invalidMessageId").innerText ="";
				$id("btnRegisterId").disabled = false;
			}
			
			
			//alert(data);
			//sessionStorage.userName=userName.substring(0,5);
		},
		failure : function(xhr) {
			console.log("fail");

		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
	}
}

function validateEmail(){
	var email =  $id("txtEmailId").value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email)) {
    	$id("invalidMessageId").innerText ="Vui lòng nhập đúng email";
		$id("btnRegisterId").disabled = true;
    }else{
    	$id("invalidMessageId").innerText ="";
		$id("btnRegisterId").disabled = false;

    }
}
